#It seems must set compiler at first
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_C_COMPILER "mips-linux-gnu-gcc")
SET(CMAKE_CXX_COMPILER "mips-linux-gnu-g++")
#SET(CMAKE_BUILD_TYPE Debug CACHE STRING "set build type to Debug") #Equal to: cmake .. -DCMAKE_BUILD_TYPE=Debug

PROJECT (SDK)
CMAKE_MINIMUM_REQUIRED(VERSION 3.0)

#Avoid out-source build, out-of-source build is supported only.
IF(PROJECT_BINARY_DIR STREQUAL PROJECT_SOURCE_DIR)
    MESSAGE(FATAL_ERROR "Do NOT support in-source build!!\n"
    "Please:\n"
    "    1. mkdir build;cd build\n"
    "    2. cmake ..\n"
    "    3. make\n")
ELSE()
    MESSAGE(STATUS "BINARY dir path: " ${PROJECT_BINARY_DIR})
    MESSAGE(STATUS "SOURCE dir path: " ${PROJECT_SOURCE_DIR})
ENDIF()

SET(SDK_VERSION $ENV{ISVP_SDK_VERSION})

EXEC_PROGRAM(git ARGS "log --format=%h-%ad, -n 1" OUTPUT_VARIABLE CMD_OUT RETURN_VALUE CMD_RET)
IF(CMD_RET)
	SET(SDK_GIT_VERSION "null")
ELSE()
	SET(SDK_GIT_VERSION ${CMD_OUT})
ENDIF()

MESSAGE(${SDK_GIT_VERSION})

EXEC_PROGRAM(${CMAKE_C_COMPILER} ARGS "-v 2>&1 | sed -n '/gcc version/p'" OUTPUT_VARIABLE CMD_OUT RETURN_VALUE CMD_RET)
IF(CMD_RET)
	SET(GCC_VERSION "null")
ELSE()
	SET(GCC_VERSION ${CMD_OUT})
ENDIF()
MESSAGE(STATUS ${GCC_VERSION})

IF(${GCC_VERSION} STREQUAL "gcc version 4.7.2 (Ingenic r2.3.3 2016.12)")
	SET(EXT_LIB_DIR "4.7.2")
ELSEIF(${GCC_VERSION} STREQUAL "gcc version 5.4.0 (Ingenic r3.3.0-gcc540 2018.04-11)")
	SET(EXT_LIB_DIR "5.4.0")
ELSEIF(${GCC_VERSION} STREQUAL "gcc version 5.4.0 (Ingenic Ingenic r3.3.0-gcc540 Smaller Size 2023.05-22)")
	SET(EXT_LIB_DIR "5.4.0")
ELSE()
    MESSAGE(FATAL_ERROR "do not support this gcc version: " ${GCC_VERSION})
	SET(EXT_LIB_DIR "null")
ENDIF()

SET(SOC "t23")
SET(LANGUAGE "cn")

SET(SOC_WHO CONFIG_SOC_T23)

SET(CMAKE_C_FLAGS "-O2 -Wall -march=mips32r2")
SET(CMAKE_CXX_FLAGS "-O2 -Wall -march=mips32r2 -std=c++11")

ADD_DEFINITIONS(-DCONFIG_SDK_VERSION="${SDK_VERSION}")
ADD_DEFINITIONS(-DCONFIG_SDK_GIT_VERSION="${SDK_GIT_VERSION}")
ADD_DEFINITIONS(-DCONFIG_SOC="${SOC}" -D${SOC_WHO})
ADD_DEFINITIONS(-DCONFIG_SINGLE_BS)

IF(NOT EXISTS $ENV{ISVP_ENV_TOP})
    MESSAGE(FATAL_ERROR "please cd to isvp top and run \"source build/t23/env_setup.sh first\"")
ENDIF()

IF(NOT $ENV{ISVP_SOC_TYPE} STREQUAL "T23")
    MESSAGE(FATAL_ERROR "please cd to isvp top and run \"source build/t23/env_setup.sh first\"")
ENDIF()

SET(CMAKE_INSTALL_PREFIX $ENV{ISVP_ENV_RELEASE_DIR_SDK})

SET(LIB_INSTALL_DIR_GLIBC lib/glibc)
SET(LIB_INSTALL_DIR_UCLIBC lib/uclibc)
SET(BIN_INSTALL_DIR_GLIBC bin/glibc)
SET(BIN_INSTALL_DIR_UCLIBC bin/uclibc)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include/api/${LANGUAGE})
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include/soc/${SOC})
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/src/imp)

option(CONFIG_DMALLOC "This is a default option for CONFIG_DMALLOC" OFF)


ADD_SUBDIRECTORY(include)
ADD_SUBDIRECTORY(src)
if(${CONFIG_DMALLOC} STREQUAL "OFF")
ADD_SUBDIRECTORY(samples)
ADD_SUBDIRECTORY(src/imp/audio/test)
endif()
